<?php

namespace app\ports\controller\publics;

use Curl;
use think\Config;
use think\Db;
use think\Log;

/**
 * 公共类
 * Class
 * @package
 */
class Publics
{

	protected static $instance;

	/**
	 * 获取实例对象
	 * @return Tables|object
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 获取MAX版本 - 域名
	 * @return array
	 */
	public function _get_http_url($sn)
	{
		$return = [];
		if (!empty($sn)) {
			$dbConfig = Config::get("notic_db");
			foreach ($dbConfig as $k => $v) {
				$connect = Db::connect($v);
				if ($connect) {
					$check = $connect->name("equip")
						->where("sn = '{$sn}'")
						->count();
					if ($check) {
						$return["tp"] = $k;
						$return["url"] = constant('\app\ports\constants\JumpConstants::' .  strtoupper($k));
						break;
					}
				}
			}
		}
		return $return;
	}

	/**
	 * 推送通知推送通知
	 *
	 * @Author HaiQing.wu
	 * @Email hqingwu@foxmail.com
	 * @DateTime 2022-05-25
	 * @param [type] $data
	 * @param [type] $update
	 * @return void
	 */
	public function sends_notic($option, $noticUrl, $sn)
	{
		try {
			if (is_array($option) && is_string($noticUrl) && !empty($noticUrl)) {
				$return_arrs = $this->_get_http_url($sn);
				if ($return_arrs) {
					$url = $return_arrs["url"] . $noticUrl;
					$curl = new Curl($url);
					$jsonData = ["data"  => json_encode($option, JSON_UNESCAPED_UNICODE)];
					$curl->post($jsonData);
				}
			}
		} catch (\Exception $e) {
			Log::write($e->getMessage());
		}
	}
}
